package com.yswl.yeeduo.base;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.tool.KRandomTool;

public class KActivity extends FragmentActivity {

	protected static ArrayList<Activity> ACTIVITYS = new ArrayList<Activity>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 渐变背景图片失真问题
		// getWindow().setFormat(PixelFormat.RGBA_8888);
		super.requestWindowFeature(Window.FEATURE_NO_TITLE);
		try {
			ACTIVITYS.add(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 
	 * @param layoutId
	 * @param fragment
	 */
	public void replaceFragment(int layoutId, Fragment fragment) {
		if(fragment==null) return;
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(layoutId, fragment).commit();
	}

	/**
	 * replace()这个方法只是在上一个Fragment不再需要时采用的简便方法。
	 * 
	 * @param newFragment
	 */
	public void replaceFragment2(int layoutId, Fragment newFragment) {
		FragmentTransaction trasection = getSupportFragmentManager().beginTransaction();
		if (!newFragment.isAdded()) {
			trasection.replace(layoutId, newFragment);
			trasection.addToBackStack(null);
			trasection.commit();
		} else {
			trasection.show(newFragment);
		}
	}

	// 实际中采用 此法切换
	public void switchContent(Fragment from, Fragment to) {
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		// .setCustomAnimations(android.R.anim.fade_in, R.anim.back_right_out)
		if (!to.isAdded()) { // 先判断是否被add过
			transaction.hide(from).add(R.id.content_frame, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
		} else {
			transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
		}
	}

	/**
	 * 创建一个空的FrameLayout
	 * 
	 * @param context
	 * @return
	 */
	public static FrameLayout createEmptyFrameLayout(Context context) {
		FrameLayout layout = new FrameLayout(context);
		layout.setId(KRandomTool.getRandom4Int(8));
		layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		return layout;
	}

	/** 根据名字找view 的资源id */
	protected int findIdByResourName(String idName) {
		String pkgName = this.getPackageName();
		return this.getResources().getIdentifier(idName, "id", pkgName);
	}

	/**
	 * 隐藏键盘
	 * 
	 * @param v
	 */
	public void hideKeyboard(View v) {
		try {
			InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			if (imm.isActive()) {
				imm.hideSoftInputFromWindow(v.getWindowToken(), 0); //
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void finish() {
		try {
			Log.e("Finish", "" + this);
			ACTIVITYS.remove(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.finish();
	}

	/**
	 * 保留当前的activity 移除所有的其他的activity
	 * @param activ
	 */
	public void removeAllActivity(Activity activ){
		Iterator<Activity> iterator = ACTIVITYS.iterator();
		while (iterator.hasNext()) {
			Activity activity = iterator.next();
			iterator.remove();
			activity.finish();
		}
		ACTIVITYS.add(activ);
	}
	
	public void exitApp() {
		Iterator<Activity> iterator = ACTIVITYS.iterator();
		MobclickAgent.onKillProcess(this);// 友盟统计 在app退出 或app 进程杀掉的时候
		while (iterator.hasNext()) {
			Activity activity = iterator.next();
			iterator.remove();
			activity.finish();
		}
	}

	// 获取应用名称
	public String getAppName() {
		return this.getResources().getString(R.string.app_name);
	}
}
